<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class ConfigController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = \DB::table("config")->get();
		return view("admin.config.index",['list'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.config.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		
        if($request->hasFile('ufile')){
			
    		//获取上传文件信息
			$file = $request->file('ufile');
			//获取上传文件的后缀名
			$ext = $file->getClientOriginalExtension();
			//随机一个新的文件名
			$filename = time().rand(1000,9999).".".$ext;
			//执行文件移动
			$newfile = $file->move("./config/uploads/",$filename);
			
			$img = new \App\Org\Image();
			$img->open("./config/uploads/{$filename}")->thumb(50,50)->save("./config/uploads/s_{$filename}");
			
			//获取要添加的数据
			$data = [
						'logo'=>$filename,
						'title'=>$request->title,
						'copy'=>$request->copy,
						'keyword'=>$request->keyword,
						'status'=>$request->status
					];
		
			//获取自增id
			$m = \DB::table("config")->insertGetId($data);
			//判断
			if($m>0){
				$info = "配置信息添加成功！";
			}else{
				$info = "配置信息添加失败！";
			}
			
			 return redirect("admin/config")->with("err",$info);
		}
	}

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
         $list = \DB::table("config")->where("id",$id)->first();
		
		return view("admin.config.edit",['list'=>$list]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        
			
			//获取要添加的数据
			$data = [
						'logo'=>$request->logo,
						'title'=>$request->title,
						'copy'=>$request->copy,
						'keyword'=>$request->keyword,
						'status'=>$request->status
					
					];
		
			//获取自增id
			$m = \DB::table("config")->where('id',$id)->update($data);
			//判断
			if($m>0){
				$info = "配置信息修改成功！";
			}else{
				$info = "配置信息修改失败！";
			}
			
			 return redirect("admin/config")->with("err",$info);
		
		
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
		
         $data = \DB::table('config')->where('id',$id)->first();
		 
		
		\DB::table("config")->delete($id);
        return redirect("admin/config")->with("err","删除成功！");
		
    }
	
	
}
